package com.project.purchase.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.purchase.Service.IProducer;
import com.project.purchase.Service.IUser;
import com.project.purchase.entity.Producer;
import com.project.purchase.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author VX5
 * @Title: QZP
 * @ProjectName purchase
 * @Description: TODO
 * @date ${DAT}20:22
 */
@Controller
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    IProducer iProducer;
    @Autowired
    IUser iUser;

    @GetMapping("/get")
    public String GetAll(HttpServletRequest request,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List list = iProducer.getAllPro();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "producer";
    }

    @RequestMapping(value = "/delet")
    public String deletByID(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize) throws ServletException, IOException {
        PageHelper.startPage(pageNum,pageSize);
        String id = request.getParameter("id");
        iProducer.delet(id);
        List list = iProducer.getAllPro();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "producer";
    }

    @PostMapping("/search")
    public String searchByName(HttpServletRequest request,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        String name = request.getParameter("search");
        List list = iProducer.GetByName(name);
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "producer";
    }

    /**
     * 增加产商
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public String add(HttpServletRequest request,
                      @RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        String id = request.getParameter("id");
        String tel = request.getParameter("tel");
        String name = request.getParameter("name");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        Producer producer = new Producer();
        producer.setAddress(address);
        producer.setLoginname(username);
        producer.setPassword(password);
        producer.setProducername(name);
        producer.setTel(tel);
        producer.setProducerid(id);
        User user = new User();
        user.setLoginname(username);
        user.setPassword(password);
        user.setParam("producer");
        user.setLoginid(id);
        iProducer.addPro(producer);
        iUser.addUser(user);
        List list = iProducer.getAllPro();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "producer";
    }

    @PostMapping("/update")
    public String updatePro(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        String id = request.getParameter("id");
        String tel = request.getParameter("tel");
        String name = request.getParameter("name");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        Producer producer = new Producer();
        producer.setAddress(address);
        producer.setLoginname(username);
        producer.setPassword(password);
        producer.setProducername(name);
        producer.setTel(tel);
        producer.setProducerid(id);
        User user = new User();
        user.setLoginname(username);
        user.setPassword(password);
        user.setParam("producer");
        iProducer.updatePro(producer);
        iUser.updateUser(user);
        PageHelper.startPage(pageNum,pageSize);
        List list = iProducer.getAllPro();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "producer";
    }
//    @PostMapping("/add")
//    public String add(HttpServletRequest request,
//                      @RequestParam(defaultValue = "1") Integer pageNum,
//                      @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
//    }
}
